import axios from 'axios';
import { commands, window } from 'vscode';
import { CommandConst } from '../../constants';
import { SystemService } from './system.service';
import { UserService } from './user.service';

/**
 * 统一接口服务接入
 *
 * @author chitanda
 * @date 2022-12-13 10:12:28
 * @class ApiService
 */
class ApiService {
  /**
   * 用户
   *
   * @author chitanda
   * @date 2022-12-13 10:12:24
   */
  readonly user = new UserService();

  /**
   * 系统
   *
   * @author chitanda
   * @date 2022-12-13 10:12:48
   */
  readonly system = new SystemService();

  constructor() {
    axios.interceptors.response.use(
      response => response,
      error => {
        const { response } = error;
        if (response) {
          if (response.status === 401) {
            commands.executeCommand(CommandConst.USER.LOGOUT);
            return Promise.resolve();
          }
          if (response.status === 403) {
            window.showWarningMessage('您暂时无权限查看此数据!');
          }
        }
        return Promise.reject(error);
      },
    );
  }
}
// 统一 api 导出
export const api = new ApiService();
